package com.neuedu.shop.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.neuedu.shop.entity.User;
import com.neuedu.shop.entity.request.PageParam;
import com.neuedu.shop.entity.request.RequestUser;
import com.neuedu.shop.entity.request.UserLogin;
import com.neuedu.shop.entity.vo.UserVO;
import com.neuedu.shop.mapper.UserMapper;
import com.neuedu.shop.service.UserService;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;

@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {
    @Override
    public boolean register(User user) {
        QueryWrapper<User> queryWrapper = new QueryWrapper();
        queryWrapper.eq("username", user.getUsername());
        List<User> userList = this.list(queryWrapper);
        if (userList.size() > 0) {
            return false;
        }
        this.save(user);
        return true;
    }

    @Override
    public boolean login(UserLogin user) {
        QueryWrapper<User> queryWrapper = new QueryWrapper();
        queryWrapper.eq("username", user.getUsername());
        queryWrapper.eq("password", user.getPassword());
        List<User> userList = this.list(queryWrapper);
        if (userList.size() > 0) {
            return true;
        }
        return false;
    }

    @Override
    public IPage<UserVO> getUsers(RequestUser requestUser) {
        Page page = new Page();
        page.setSize(requestUser.getPageSize());
        page.setCurrent(requestUser.getCurrentPage());
        // 分页查询

        QueryWrapper queryWrapper=new QueryWrapper();
        if(!StringUtils.isEmpty(requestUser.getUsername())) {
            queryWrapper.like("username", requestUser.getUsername());
        }

        if(!StringUtils.isEmpty(requestUser.getSex())){
            queryWrapper.eq("sex",requestUser.getSex());
        }

        IPage<User> users = this.page(page,queryWrapper);
        List<User> list = users.getRecords();
        // 分页转换
        List<UserVO> userVOS = new ArrayList<>();
        for (User user : list) {
            UserVO userVO = new UserVO();
            userVO.setId(user.getId());
            userVO.setUsername(user.getUsername());
            userVO.setSex(user.getSex());
            userVO.setBirthday(user.getBirthday());
            userVOS.add(userVO);
        }

        IPage<UserVO>  userVOIPage=new Page<>();
        userVOIPage.setCurrent(users.getCurrent());
        userVOIPage.setSize(users.getSize());
        userVOIPage.setTotal(users.getTotal());
        userVOIPage.setPages(users.getPages());
        userVOIPage.setRecords(userVOS);
        return userVOIPage;
    }
    // List<MenuVO>
    // 1. 查出所有菜单记录
    // 2. 判定父级ID为0
    // foreach循环:if(parentId==id)
    // 3. 参数：所有菜单数据，父类id
}
